//
//  AiRoleItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/4/1.
//

import SwiftUI
import Kingfisher

/**
 *  ai 角色item
 */
struct AiRoleItemView: View {
    @State var delTips = false
    @StateObject var viewModel = AiRoleItemViewModel()
    @State private var showContent = false
    @State private var select = false
    
    var bean:AiRoleBean
    @Binding var items:[AiRoleBean]

    var body: some View {
        HStack(alignment:.top) {
            KFImage(URL(string: bean.avatar.formatCdn()))
                .resizable()
                .placeholder{
                    Image(.appDefaultLogo)
                        .resizable()
                        .scaledToFill()
                        .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                        .background(Color.hex("515151").opacity(0.1))
                        .clipShape(Circle())
                }
                .scaledToFill()
                .clipShape(Circle())
                .frame(width: 50,height: 50)
                .contentShape(Rectangle())
                .onTapGesture {
                    pathManager.jump(type: JumpType.AI_ROLE, data: bean)
                }
            VStack(alignment:.leading){
                Text(bean.name ?? "").font(.system(size: 14)).foregroundColor(.bgNormalText2)
                Text(bean.describe ?? "").font(.system(size: 12)).foregroundColor(.bgNormalText4).lineLimit(2)
            }
        }.frame(maxWidth: .infinity,alignment:.leading)
            .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
            .background(themeManager.themeColor.opacity(select ? 0.1 : 0))
            .contentShape(Rectangle())
            .onAppear{
                select = bean.select
            }
            .contextMenu {
                Button("查看"){
                    showContent.toggle()
                }
                if bean.select {
                    Button("取消选择") {
                        if items.count(where: {$0.select}) <= 1 {
                            showLog("至少选择一个")
                            showToast("至少选择一个")
                        }
                        bean.select = false
                        viewModel.selectAiRole(list: items) {
                            select = false
                            //取消选择，需要删除任务
                           TaskExt.deleteTask(bean.objectId)
                        }
                    }
                } else {
                    Button("选择") {
                        if items.count(where: {$0.select}) > 5 {
                            showLog("最多只能选择5个")
                            showToast("最多只能选择5个")
                        }
                        bean.select = true
                        viewModel.selectAiRole(list: items){
                            select = true
                        }
                    }
                }
                if bean.isSystem ?? false == false {
                    Button("修改") {
                        pathManager.jump(type: JumpType.AI_ROLE, data: bean)
                    }
                }
                if bean.isSystem ?? false == false {
                    Button("删除") {
                        delTips.toggle()
                    }
                }
            }
            .alert("温馨提示",isPresented:$delTips) {
                Button("取消",role: .cancel){}
                Button("确定",role: .destructive) {
                    viewModel.doDelete(bean: bean)
                }
            } message: {
                Text("是否确定删除该角色")
            }
            .sheet(isPresented: $showContent) {
                Text("\(bean.describe ?? "")\n\n\n\(bean.describe2 ?? "")")
                    .font(.system(size: 14))
                    .foregroundStyle(.bgNormalText2)
                    .padding()
                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
            }.presentationDragIndicator(.visible)
    }
    
}

#Preview {
    @State var previewItems: [AiRoleBean] = [AiRoleBean(), AiRoleBean()]
    AiRoleItemView(bean:AiRoleBean(), items: $previewItems)
}
